package com.xfp.controller;

import com.xfp.result.Result;
import com.xfp.pojo.dto.CategoryDto;
import com.xfp.pojo.dto.CategoryQueryDto;
import com.xfp.pojo.entity.Category;
import com.xfp.pojo.vo.CategoryQueryVo;
import com.xfp.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/vm-service/skuClass")
@Slf4j
@Api(tags = "商品类别")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param categoryDto
     * @return
     */
    @PostMapping
    @ApiOperation("商品类别")
    public Result<Boolean> insert(@RequestBody CategoryDto categoryDto) throws Exception {
        log.info("商品类别：{}",categoryDto);
        Boolean b = categoryService.insert(categoryDto);
        return Result.success(b);
    }

    /**
     * 分类分页查询
     * @param categoryQueryDto
     * @return
     */
    @GetMapping("/search")
    @ApiOperation("商品分类分页查询")
    public CategoryQueryVo page(CategoryQueryDto categoryQueryDto){
        log.info("商品分类分页查询：{}",categoryQueryDto);
        CategoryQueryVo categoryQueryVo = categoryService.pageQuery(categoryQueryDto);
        return categoryQueryVo;
    }

    /**
     * 根据id获取基本信息
     * @param classId
     * @return
     * @throws Exception
     */
    @GetMapping("/{classId}")
    @ApiOperation("获取商品分类信息")
    public Category findById(@PathVariable Integer classId) throws Exception {
        log.info("获取商品分类信息：{}",classId);
        Category category = categoryService.findById(classId);
        return category;
    }


    /**
     * 更新分类信息
     * @param categoryDto
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation("更新分类")
    public Result<Boolean> update(@PathVariable Integer id,@RequestBody CategoryDto categoryDto) throws Exception {
        log.info("更新分类信息:{},{}",id,categoryDto);
        Boolean b = categoryService.update(id,categoryDto);
        return Result.success(b);
    }

    /**
     * 根据id删除分类
     * @param classId
     * @return
     */
    @DeleteMapping("/{classId}")
    @ApiOperation("删除分类")
    public void deleteById(@PathVariable Integer classId){
        log.info("根据id删除分类：{}",classId);
       categoryService.deleteById(classId);
    }




}
